//
//  JJReactNativeService.m
//  ClaimPlatform_RN
//
//  Created by hamilyjing on 06/04/2018.
//  Copyright © 2018 jj. All rights reserved.
//

#import "JJReactNativeService.h"

#import <YTKNetwork/YTKNetwork.h>

#import "JJReactNativeRequest.h"
#import "JJReactNativeModel.h"
#import "JJServiceManager.h"
#import "JJBaseRequest.h"

@implementation JJReactNativeService

- (void)requestRNWithOptions:(NSDictionary *)options
    networkSuccessResponse:(void (^)(JJReactNativeModel *object, id otherinfo))networkSuccessResponse
       networkFailResponse:(void (^)(NSError *error, id otherInfo))networkFailResponse
{
    JJBaseRequest *request = [self __requestObjectRNWithOptions:options];
    request.networkSuccessResponse = networkSuccessResponse;
    request.networkFailResponse = networkFailResponse;
    
    NSDictionary *otherInfo = options[@"otherInfo"];
    [JJ_SERVICE_FEATURE_SET(self, JJFeatureSet) startRequest:request otherInfo:otherInfo];
}

- (NSString *)cacheRNWithOptions:(NSDictionary *)options
{
    JJBaseRequest *request = [self __requestObjectRNWithOptions:options];
    JJReactNativeModel *model = [request cacheModel];
    NSString *result = model.rnDataContentStr;
    return result;
}

- (void)saveCacheRNWithOptions:(NSDictionary *)options obj:(NSString *)obj
{
    JJBaseRequest *request = [self __requestObjectRNWithOptions:options];
    JJReactNativeModel *model = [[JJReactNativeModel alloc] init];
    model.rnDataContentStr = obj;
    [request saveObjectToDiskCache:model];
}

- (void)removeCacheRNWithOptions:(NSDictionary *)options
{
    JJBaseRequest *request = [self __requestObjectRNWithOptions:options];
    [request removeAllCache];
}

- (JJBaseRequest *)requestForReactNativeWithIdentity:(NSString *)identity
                                           parameters:(NSDictionary *)parameters
                                         isEncryption:(BOOL)isEncryption
                                           modelClass:(Class)modelClass
                                       isSaveToMemory:(BOOL)isSaveToMemory
                                         isSaveToDisk:(BOOL)isSaveToDisk
{
    JJBaseRequest *request = [[JJReactNativeRequest alloc] initWithIdentity:identity parameters:parameters isEncryption:isEncryption modelClass:[JJReactNativeModel class] isSaveToMemory:NO isSaveToDisk:isSaveToDisk];
    return request;
}

#pragma mark - private

- (JJBaseRequest *)__requestObjectRNWithOptions:(NSDictionary *)options
{
    NSString *userCacheDirectory = options[@"userCacheDirectory"];
    NSString *identity = options[@"identity"];
    NSString *methodType = options[@"methodType"];
    BOOL isEncryption = options[@"isEncryption"] ? [options[@"isEncryption"] boolValue] : NO;
    NSDictionary *parameters = options[@"parameters"];
    BOOL isSaveToDisk = options[@"isSaveToDisk"] ? [options[@"isSaveToDisk"] boolValue] : NO;
    NSDictionary *parametersForSavedFileName = options[@"parametersForSavedFileName"];
    NSString *sensitiveDataForSavedFileName = options[@"sensitiveDataForSavedFileName"];
    
    JJBaseRequest *request = [self requestForReactNativeWithIdentity:identity parameters:parameters isEncryption:isEncryption modelClass:[JJReactNativeModel class] isSaveToMemory:NO isSaveToDisk:isSaveToDisk];
    
    if (userCacheDirectory && ![userCacheDirectory isEqualToString:@""])
    {
        request.userCacheDirectory = userCacheDirectory;
    }
    else
    {
        request.userCacheDirectory = @"JJReactNativeService";
    }
    
    if (parametersForSavedFileName)
    {
        request.parametersForSavedFileName = parametersForSavedFileName;
    }
    
    request.requestMethodType = YTKRequestMethodPOST;
    if ([[methodType lowercaseString] isEqualToString:@"get"])
    {
        request.requestMethodType = YTKRequestMethodGET;
    }
    
    if (sensitiveDataForSavedFileName)
    {
        request.sensitiveDataForSavedFileName = sensitiveDataForSavedFileName;
    }
    
    return request;
}

@end
